/*
 * @Author: maskMan
 * @LastEditTime: 2024-03-29 19:05:18
 * @Descripttion:存储管理
 */
import { sys } from 'cc'
import { STORAGE_KEY } from '../define/constants'
import util from './util'
function _decorator(target: typeof CacheStorage) {
  target._readData()
}
@_decorator
export default class CacheStorage {
  static _publicData = {} //公共数据
  static _userData = {} //用户数据
  static _configData = {} //配置数据
  static _redordData = {} //记录数据
  /**初始化读取存储 */
  static _readData() {
    for (let i = 0; i < sys.localStorage.length; i++) {
      const key: string = sys.localStorage.key(i)
      try {
        const ret = sys.localStorage.getItem(key)
        if (ret) {
          if (key == STORAGE_KEY.CONFIG) {
            this._configData = JSON.parse(ret)
          } else if (key == STORAGE_KEY.PUBLIC) {
            this._publicData = JSON.parse(ret)
          } else if (key == STORAGE_KEY.USER) {
            this._userData = JSON.parse(ret)
          } else if (key == STORAGE_KEY.RECORD) {
            this._redordData = JSON.parse(ret)
          }
        }
      } catch (err) {
        console.error('缓存读取出错', err)
      }
    }
  }
  /**
   * @description:删除存储数据
   * @param {string} key 存储键
   * @return {*}
   */
  static delData(key: string, mark: STORAGE_KEY) {
    if (mark == STORAGE_KEY.CONFIG) {
      delete this._configData[key]
      sys.localStorage.setItem(mark, JSON.stringify(this._configData))
    } else if (mark == STORAGE_KEY.PUBLIC) {
      delete this._publicData[key]
      sys.localStorage.setItem(mark, JSON.stringify(this._publicData))
    } else if (mark == STORAGE_KEY.USER) {
      delete this._userData[key]
      sys.localStorage.setItem(mark, JSON.stringify(this._userData))
    } else if (mark == STORAGE_KEY.RECORD) {
      delete this._redordData[key]
      sys.localStorage.setItem(mark, JSON.stringify(this._redordData))
    }
  }
  /**
   * @description:清理缓存数据
   * @param {string} key 存储键
   * @return {*}
   */
  static clearData(mark?: STORAGE_KEY) {
    if (!mark) {
      this._configData = {}
      this._publicData = {}
      this._userData = {}
      this._redordData = {}
      sys.localStorage.clear()
      return
    }
    if (mark == STORAGE_KEY.CONFIG) {
      this._configData = {}
    } else if (mark == STORAGE_KEY.PUBLIC) {
      this._publicData = {}
    } else if (mark == STORAGE_KEY.USER) {
      this._userData = {}
    } else if (mark == STORAGE_KEY.RECORD) {
      this._redordData = {}
    }
    sys.localStorage.removeItem(mark)
  }
  /**
   * @description:设置公共存储数据
   * @param {string} key 键名
   * @param {any} value 键值
   */
  static setPublicData(key: string, value: any) {
    try {
      const mark = STORAGE_KEY.PUBLIC
      this._publicData[key] = value
      sys.localStorage.setItem(mark, JSON.stringify(this._publicData))
    } catch (error) {
      console.error('缓存设置出错', error)
    }
  }
  /**
   * @description:设置用户存储数据
   * @param {string} uid 用户ID
   * @param {Obj} user 用户信息
   */
  static setUserData(uid: string, user: Obj) {
    try {
      const mark = STORAGE_KEY.USER
      this._userData[uid] = user
      sys.localStorage.setItem(mark, JSON.stringify(this._userData))
    } catch (error) {
      console.error('缓存设置出错', error)
    }
  }
  /**
   * @description:设置配置存储数据
   * @param {string} key 配置键
   * @param {Obj} data 配置数据
   */
  static setConfigData(key: string, data: any) {
    try {
      const mark = STORAGE_KEY.CONFIG
      this._configData[key] = data
      sys.localStorage.setItem(mark, JSON.stringify(this._configData))
    } catch (error) {
      console.error('缓存设置出错', error)
    }
  }
  /**
   * @description:设置用户存储数据
   * @param {string} uid 所属用户ID
   * @param {Obj} data 数据
   */
  static setRecordData(uid: string, data: Obj): IStorageType {
    try {
      const mark = STORAGE_KEY.RECORD
      const time = util.now()
      const obj: IStorageType = {
        _uid: uid,
        _id: time,
        data
      }
      this._redordData[time] = obj
      sys.localStorage.setItem(mark, JSON.stringify(this._redordData))
      return obj
    } catch (error) {
      console.error('缓存设置出错', error)
      return null
    }
  }
  /**
   * @description:更新用户存储数据
   * @param {number} _id 数据ID
   * @param {Obj} data 数据
   */
  static updateRecordData(_id: number, data: Obj): boolean {
    try {
      const mark = STORAGE_KEY.RECORD
      if (this._redordData[_id]) {
        this._redordData[_id]['data'] = data
        sys.localStorage.setItem(mark, JSON.stringify(this._redordData))
        return true
      }
      return false
    } catch (error) {
      console.error('更新缓存设置出错', error)
      return false
    }
  }
  /**
   * @description:获取用户数据
   * @param {string} key 键名
   */
  static getPublicData(key?: string): any {
    try {
      const result = key ? this._publicData[key] : this._publicData
      return result || null
    } catch (error) {
      return null
    }
  }
  /**
   * @description:获取配置数据
   * @param {string} key 键名
   */
  static getConfigData(key?: string): any {
    try {
      const result = key ? this._configData[key] : this._configData
      return result || null
    } catch (error) {
      return null
    }
  }
  /**
   * @description:获取用户数据
   * @param {string} uid 用户id
   */
  static getUserData(uid?: string): Obj | Obj[] {
    try {
      if (uid) {
        return this._userData[uid] || null
      }
      return util.objectToArray(this._userData)
    } catch (error) {
      console.error('获取缓存出错', error)
      return null
    }
  }
  /**
   * @description:获取记录数据
   * @param {string} _id id
   */
  static getRecordData(_id?: number): Obj | Obj[] {
    try {
      if (_id) {
        return this._redordData[_id] || null
      }
      return util.objectToArray(this._redordData)
    } catch (error) {
      console.error('获取缓存出错', error)
      return null
    }
  }
}
